package cn.iocoder.yudao.framework.common.util.http;

import cn.hutool.core.map.TableMap;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import sun.net.www.http.HttpClient;

import javax.annotation.Nullable;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * HTTP 工具类
 *
 * @author 芋道源码
 */
public class HttpUtils {

    @SuppressWarnings("unchecked")
    public static String replaceUrlQuery(String url, String key, String value) {
        UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset());
        // 先移除
        TableMap<CharSequence, CharSequence> query = (TableMap<CharSequence, CharSequence>)
                ReflectUtil.getFieldValue(builder.getQuery(), "query");
        query.remove(key);
        // 后添加
        builder.addQuery(key, value);
        return builder.build();
    }


        /**
         * Http get请求
         * @param httpUrl 连接
         * @return 响应数据
         */
        public static String doGet(String httpUrl){
            //链接
            HttpURLConnection connection = null;
            InputStream is = null;
            BufferedReader br = null;
            StringBuffer result = new StringBuffer();
            try {
                //创建连接
                URL url = new URL(httpUrl);
                connection = (HttpURLConnection) url.openConnection();
                //设置请求方式
                connection.setRequestMethod("GET");
                //设置连接超时时间
                connection.setReadTimeout(15000);
                //开始连接
                connection.connect();
                //获取响应数据
                if (connection.getResponseCode() == 200) {
                    //获取返回的数据
                    is = connection.getInputStream();
                    if (null != is) {
                        br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                        String temp = null;
                        while (null != (temp = br.readLine())) {
                            result.append(temp);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //关闭远程连接
                connection.disconnect();
            }
            return result.toString();
        }

        /**
         * Http post请求
         * @param httpUrl 连接
         * @param param 参数
         * @return
         */
        public static String doPost(String httpUrl, @Nullable String param) {
            StringBuffer result = new StringBuffer();
            //连接
            HttpURLConnection connection = null;
            OutputStream os = null;
            InputStream is = null;
            BufferedReader br = null;
            try {
                //创建连接对象
                URL url = new URL(httpUrl);
                //创建连接
                connection = (HttpURLConnection) url.openConnection();
                //设置请求方法
                connection.setRequestMethod("POST");
                //设置连接超时时间
                connection.setConnectTimeout(15000);
                //设置读取超时时间
                connection.setReadTimeout(15000);
                //DoOutput设置是否向httpUrlConnection输出，DoInput设置是否从httpUrlConnection读入，此外发送post请求必须设置这两个
                //设置是否可读取
                connection.setDoOutput(true);
                connection.setDoInput(true);
                //设置通用的请求属性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

                //拼装参数
                if (null != param && !param.equals("")) {
                    //设置参数
                    os = connection.getOutputStream();
                    //拼装参数
                    os.write(param.getBytes("UTF-8"));
                    os.flush();
                }
                //设置权限
                //设置请求头等
                //开启连接
                //connection.connect();
                //读取响应
                if (connection.getResponseCode() == 200) {
                    is = connection.getInputStream();
                    if (null != is) {
                        br = new BufferedReader(new InputStreamReader(is, "GBK"));
                        String temp = null;
                        while (null != (temp = br.readLine())) {
                            result.append(temp);
                            result.append("\r\n");
                        }
                    }
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭连接
                if(br!=null){
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(os!=null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //关闭连接
                connection.disconnect();
            }
            return result.toString();
        }

    public static String doGet(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) {
        //链接
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            String param=paramsConvertUrl(querys);
            //创建连接
            URL url = new URL(host+path+param);
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod(method);
            //设置连接超时时间
            connection.setReadTimeout(15000);
            //开始连接
            connection.connect();
            //获取响应数据
            if (connection.getResponseCode() == 200) {
                //获取返回的数据
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭远程连接
            connection.disconnect();
        }
        return result.toString();
    }

    private static String paramsConvertUrl(Map<String, String> params) {
        StringBuilder urlParams = new StringBuilder("?");
        Set<Map.Entry<String, String>> entries = params.entrySet();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            urlParams.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        String urlParamsStr = urlParams.toString();
        return urlParamsStr.substring(0, urlParamsStr.length()-1);
    }
}
